package com.examination.system.administrator.service.impl;

import com.examination.framework.exception.ExceptionCast;
import com.examination.framework.model.response.QueryResponseResult;
import com.examination.framework.model.response.QueryResult;
import com.examination.framework.model.response.ResponseResult;
import com.examination.system.administration.Nation;
import com.examination.system.administration.response.AddAdministrationResult;
import com.examination.system.administration.response.DeleteAdministrationResult;
import com.examination.system.administration.response.NationCode;
import com.examination.system.administrator.dao.NationMapper;
import com.examination.system.administrator.dao.NationRepository;
import com.examination.system.administrator.service.NationService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * @author 寇淇芮
 * @Date 2019/8/24 22:00
 */
@Service
public class NationServiceImpl implements NationService {
    @Autowired
    private NationRepository nationRepository;
    @Autowired
    private NationMapper nationMapper;
    @Override
    @Transactional
    public AddAdministrationResult addNation(Nation nation) {

        //先判断是否有数据
        if(nation==null|| StringUtils.isEmpty(nation.getNation().trim())){
            ExceptionCast.cast(NationCode.WITHSEVERALEXCEPTIONS);
        }
        //判断是否有改条数据
       if(nationRepository.findByNation(nation.getNation()).size()>0){
           ExceptionCast.cast(NationCode.OCCUPATIONNATIONNAME);
       }
        //补充数据
         //补全id
        nation.setId(UUID.randomUUID().toString());
        //保存
        Nation save = nationRepository.save(nation);
        return save!=null?new AddAdministrationResult(NationCode.SUCCESS,save.getId()):new AddAdministrationResult(NationCode.FAIL,nation.getNation());
    }

    @Override
    @Transactional
    public DeleteAdministrationResult deleteNation(String id) {
        return nationMapper.delectNationById(id)>0?new DeleteAdministrationResult(NationCode.SUCCESS,id):new DeleteAdministrationResult(NationCode.FAIL,id);
    }

    @Override
    @Transactional
    public ResponseResult editNation(String id, Nation nation) {
        //判断
        if(StringUtils.isEmpty(id)||nation==null ){
            ExceptionCast.cast(NationCode.WITHSEVERALEXCEPTIONS);
        }
        /**
         * 这里可以是抛出异常,或者给一个生成id的方法
         */
        if(nation.getId().trim().equals("")){
            ExceptionCast.cast(NationCode.NATIONBYIDNULL);
        }
        /**
         * 判断用户是否输入空白字符或者是没有填写的情况
         */
        if(nation.getNation().trim().equals("")){
            ExceptionCast.cast(NationCode.NATIONBYNAMENULL);
        }
        /**
         * 判断是否存在该数据
         */
        if(!nationRepository.findById(id).isPresent()){
            ExceptionCast.cast(NationCode.DATADOESNOTEXIST);
        }


        Nation editNation = new Nation();
        //备份数据且去掉前后空格
        editNation.setId(nation.getId().trim());
        editNation.setNation(nation.getNation().trim());
        return nationRepository.save(editNation)!=null?new ResponseResult(NationCode.SUCCESS):new ResponseResult(NationCode.FAIL);
    }

    @Override
    public QueryResponseResult findNationList(Integer page,Integer size) {
        int minSize = 10;
        int maxSize = 25;
        int minPage = 1;
        if (page<minPage){
            page = 1;
        }
        if(size<minSize || size > maxSize){
            size = 10;
        }
        //设置页码和显示条数
        PageHelper.startPage(page,size);
        //查询数据
        Page<Nation> nationPage = nationMapper.findAll();
        //回写
        QueryResult queryResult = new QueryResult();
        //显示的记录
        queryResult.setList(nationPage.getResult());
        //一共的条数
        queryResult.setTotal(nationPage.getTotal());
        //返回数据
        return new QueryResponseResult(NationCode.SUCCESS,queryResult) ;
    }

    @Override
    public QueryResponseResult<Nation> findNationAll() {
        List<Nation> nationList = nationRepository.findAll();
        //回写
        QueryResult queryResult = new QueryResult();
        queryResult.setTotal((long) nationList.size());
        queryResult.setList(nationList);
        return  new QueryResponseResult(NationCode.SUCCESS,queryResult);
    }
}
